<?php

namespace tools\util;

class Time
{


    /**
     * 返回今日开始和结束的时间戳
     * @return array
     */
    public static function today()
    {
        list($y, $m, $d) = explode('-', date('Y-m-d'));
        return [
            mktime(0, 0, 0, $m, $d, $y),
            mktime(23, 59, 59, $m, $d, $y)
        ];
    }

    /**
     * 返回昨日开始和结束的时间戳
     * @return array
     */
    public static function yesterday()
    {
        $yesterday = date('d') - 1;
        return [
            mktime(0, 0, 0, date('m'), $yesterday, date('Y')),
            mktime(23, 59, 59, date('m'), $yesterday, date('Y'))
        ];
    }

    /**
     * 返回本周开始和结束的时间戳
     * @return array
     */
    public static function week()
    {
        list($y, $m, $d, $w) = explode('-', date('Y-m-d-w'));
        if($w == 0) $w = 7; //修正周日的问题
        return [
            mktime(0, 0, 0, $m, $d - $w + 1, $y),
            mktime(23, 59, 59, $m, $d - $w + 7, $y)
        ];
    }

    /**
     * 返回上周开始和结束的时间戳
     * @return array
     */
    public static function lastWeek()
    {
        $timestamp = time();
        return [
            strtotime(date('Y-m-d', strtotime("last week Monday", $timestamp))),
            strtotime(date('Y-m-d', strtotime("last week Sunday", $timestamp))) + 24 * 3600 - 1
        ];
    }

    /**
     * 返回本月开始和结束的时间戳
     * @return array
     */
    public static function month()
    {
        list($y, $m, $t) = explode('-', date('Y-m-t'));
        return [
            mktime(0, 0, 0, $m, 1, $y),
            mktime(23, 59, 59, $m, $t, $y)
        ];
    }

    /**
     * 返回上个月开始和结束的时间戳
     *
     * @return array
     */
    public static function lastMonth()
    {
        $y = date('Y');
        $m = date('m');
        $begin = mktime(0, 0, 0, $m - 1, 1, $y);
        $end = mktime(23, 59, 59, $m - 1, date('t', $begin), $y);

        return [$begin, $end];
    }

    /**
     * 返回今年开始和结束的时间戳
     * @return array
     */
    public static function year()
    {
        $y = date('Y');
        return [
            mktime(0, 0, 0, 1, 1, $y),
            mktime(23, 59, 59, 12, 31, $y)
        ];
    }

    /**
     * 返回去年开始和结束的时间戳
     *
     * @return array
     */
    public static function lastYear()
    {
        $year = date('Y') - 1;
        return [
            mktime(0, 0, 0, 1, 1, $year),
            mktime(23, 59, 59, 12, 31, $year)
        ];
    }


    /**
     * 获取几天前零点到现在/昨日结束的时间戳
     * @param int $day 天数
     * @param bool $now 返回现在或者昨天结束时间戳
     * @return array
     */
    public static function dayToNow($day = 1, $now = true)
    {
        $end = time();
        if (!$now) {
            list($foo, $end) = self::yesterday();
        }

        return [
            mktime(0, 0, 0, date('m'), date('d') - $day, date('Y')),
            $end
        ];
    }

    /**
     * 返回几天前的时间戳
     * @param int $day
     * @return int
     */
    public static function daysAgo($day = 1)
    {
        $nowTime = time();
        return $nowTime - self::daysToSecond($day);
    }

    /**
     * 返回几天后的时间戳
     * @param int $day
     * @return int
     */
    public static function daysAfter($day = 1)
    {
        $nowTime = time();
        return $nowTime + self::daysToSecond($day);
    }

    /**
     * 天数转换成秒数
     * @param int $day
     * @return int
     */
    public static function daysToSecond($day = 1)
    {
        return $day * 86400;
    }

    /**
     * 周数转换成秒数
     *
     * @param int $week
     * @return int
     */
    public static function weekToSecond($week = 1)
    {
        return self::daysToSecond() * 7 * $week;
    }


    /**
     * 计算本月最大连续天数
     * @param $day_list
     * @return int
     */
    public static function getContinueDay($day_list)
    {
        //昨天开始时间戳
        $beginYesterday=mktime(0,0,0,date('m'),date('d')-1,date('Y'));

        $days=$beginYesterday>$day_list[0]?0:1;
        $count = count($day_list);
        for($i=0;$i<$count;$i++){
            if($i<$count-1){
                $res = static::compareDay($day_list[$i],$day_list[$i+1]);
                if($res) $days++;
                else break;
            }
        }

        return $days;
    }

    private static function compareDay($curDay,$nextDay)
    {
        $lastBegin = mktime(0,0,0,date('m',$curDay),date('d',$curDay)-1,date('Y',$curDay));
        $lastEnd   = mktime(0,0,0,date('m',$curDay),date('d',$curDay),date('Y',$curDay))-1;

        if($nextDay>=$lastBegin && $nextDay<=$lastEnd){
            return true;
        }else{
            return false;
        }
    }




    /**
     * 计算本月最大连续天数
     * @param array $time_array
     * @return mixed
     */
    public static function continueDays($time_array = [])
    {
        $list_length    = count($time_array);
        $continue_days  = 1;

        $continue_days_array = [];

        for ($i = 0; $i < $list_length; $i++) {

            $today = strtotime($time_array[$i]);
            if ($i == $list_length - 1) {

                $continue_days_array[] = $continue_days;
            } else {

                $yesterday = strtotime($time_array[$i + 1]);
                $one_day = 24 * 3600;
                if ($today - $yesterday == $one_day) {
                    $continue_days += 1;
                } else {
                    $continue_days_array[] = $continue_days;
                    $continue_days = 1;
                }
            }
        }
        $max_days = max($continue_days_array);
        return $max_days;
    }

    /**
     * 获取一周具体日期
     * @param string $time
     * @param string $format
     * @return array
     */
    public static function getWeeks($time = '', $format='m.d'){
        date_default_timezone_set('Etc/GMT-8');
        $time = $time != '' ? $time : time();
        //获取当前周几
        $week = date('w', $time)==0?7:date('w', $time);

        $date = [];
        for ($i=1; $i<=7; $i++){
            $date[$i] = date($format ,strtotime( '+' . $i-$week .' days', $time));
        }

        return $date;
    }
}
